********Table J.1 - Indonesia********

eststo clear

*Load data and create differences
cd "$reploc/data/created_data"
use indo_full, clear

sort pidlink ifls
drop if ifls==.
keep if panel==1

keep rbuc*  gambleaverse ifls pidlink inflation imp_age panel biryear_impl grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co? grbrprovSD_lam0co? birprov_impl *UB* *LB*



*Compute Hausdorff metric between intervals, using CDF normal distribution over R as value at each point
local xvars income_pc_monthly NarrowBracket  

foreach x in `xvars' {

gen `x'_midInt=.
replace `x'_midInt=(`x'_LB_A+`x'_UB_A)/2 if `x'_LB_A!=-9999999 & `x'_UB_A!=9999999
sum `x'_midInt
local muse=r(mean)
local sduse=r(sd)

gen `x'_UB_cdf=normal((`x'_UB_A-`muse')/`sduse')
replace `x'_UB_cdf=1 if `x'_UB_A==9999999
gen `x'_LB_cdf=normal((`x'_LB_A-`muse')/`sduse')
replace `x'_LB_cdf=0 if `x'_UB_A==-9999999

gen `x'_distUB=abs(`x'_UB_cdf-`x'_UB_cdf[_n-1]) if ifls==5
gen `x'_distLB=abs(`x'_LB_cdf-`x'_LB_cdf[_n-1]) if ifls==5
egen `x'_distmax=rowmax(`x'_distUB `x'_distLB) if ifls==5

gen `x'_flagmax=0
replace `x'_flagmax=1 if `x'_distUB>=`x'_distLB
replace `x'_flagmax=2 if `x'_distUB<`x'_distLB

gen `x'_distuse=.
replace `x'_distuse=`x'_distmax if ifls==5 & `x'_flagmax==1 & `x'_UB_cdf>=`x'_UB_cdf[_n-1]
replace `x'_distuse=-`x'_distmax if ifls==5 & `x'_flagmax==1 & `x'_UB_cdf<`x'_UB_cdf[_n-1]
replace `x'_distuse=`x'_distmax if ifls==5 & `x'_flagmax==2 & `x'_LB_cdf>=`x'_LB_cdf[_n-1]
replace `x'_distuse=-`x'_distmax if ifls==5 & `x'_flagmax==2 & `x'_LB_cdf<`x'_LB_cdf[_n-1]
replace `x'_distuse=0 if ifls==4 & `x'_distuse[_n+1]!=.

}

drop *flagmax* *distmax* *midInt*


*Compute elements to use as mid-point
foreach x in `xvars' {

gen `x'_midIntuse=.
replace `x'_midIntuse=(`x'_LB_A+`x'_UB_A)/2 if `x'_LB_A!=-9999999 & `x'_UB_A!=9999999

gen `x'_distublbmid=`x'_midIntuse-`x'_LB_A if `x'_LB_A!=-9999999 & `x'_UB_A!=9999999
sum `x'_distublbmid, d
local avgdist=r(mean)

replace `x'_midIntuse=`x'_LB_A+`avgdist' if `x'_LB_A!=-9999999 & `x'_UB_A==9999999
replace `x'_midIntuse=`x'_UB_A-`avgdist' if `x'_LB_A==-9999999 & `x'_UB_A!=9999999

}

drop *distublbmid*


reshape wide rbuc*  gambleaverse inflation imp_age panel grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co? grbrprovSD_lam0co? *_distuse *_midIntuse *UB* *LB*, i(pidlink) j(ifls)


local varlist1 rbuc rbucB rbuc_nga rbuc_garefined rbucgaref1a rbucgaref2a rbucgaref3a rbucgaref4a gambleaverse inflation grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co2 grbrprovSD_lam0co2 grbrprovAvg_lam0co3 grbrprovSD_lam0co3 income_pc_monthly_midIntuse NarrowBracket_midIntuse income_pc_monthly_distuse NarrowBracket_distuse


foreach var in `varlist1'  {
gen `var'dif=.
replace `var'dif=`var'5-`var'4
}

*Generate cohort by birthyear variables to cluster on
egen coh_prov_clust=group(biryear_impl birprov_impl)

*Do these results for primary sample
gen primarysample1=0
replace primarysample1=1 if rbuc_garefineddif!=. & grbrprovAvg_lam0co3dif!=. & grbrprovSD_lam0co3dif!=. & inflationdif!=. &  biryear_impl>=1961 & coh_prov_clust!=.

egen primarysample=total(primarysample1), by(pidlink)
keep if primarysample==1

*Regressions
local depvar income_pc_monthly_midIntuse NarrowBracket_midIntuse income_pc_monthly_distuse NarrowBracket_distuse
local rhsvari oldindoonly
local rhsvaruse _lam0co3dif
local usei grbr


foreach x in `depvar' {

preserve

drop if `x'dif==.

eststo:  reg `x'dif  `usei'provAvg`rhsvaruse' `usei'provSD`rhsvaruse'  inflationdif, vce(cluster coh_prov_clust)

restore

}

cd "$reploc/results"	
esttab using TableJ1_Indonesia.tex, star(* 0.1 ** 0.05 *** 0.01) b(2) se(2) replace
